package sostats.core.help;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import sostats.core.datasource.Configuration;
import sostats.core.datasource.DataSource;

public class ConfigurationHelper {
	public static Configuration readProperites(String resources) throws IOException{
		InputStream in = null;
		try{
			final Properties properities = new Properties(); 
			in =ConfigurationHelper.class.getClassLoader().getResourceAsStream(resources); 
			properities.load(in);
			final DataSource ds = new DataSource(
					properities.getProperty("url"),
					properities.getProperty("port"), 
					properities.getProperty("username"), 
					properities.getProperty("password"), 
					properities.getProperty("database"), 
					properities.getProperty("type"));
			return new Configuration() {
				
				@Override
				public String getSchema() {
					return properities.getProperty("schema");
				}
				
				@Override
				public DataSource getDataSource() {
					return ds;
				}
				
				@Override
				public String getCatalog() {
					return properities.getProperty("catalog");
				}
			};
		}finally{
			if(in!=null) in.close();
		}
		
	}
}
